package com.gmrz.util;

//import org.apache.http.HttpEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.mime.MultipartEntityBuilder;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.util.Map;
//
///**
// * Created by gongyuqiang on 2017/5/8.
// */
public class HttpUtil {
//    private static final Logger LOG = LogManager.getLogger(HttpUtil.class);
//    public static String post(String postUrl, String data, String contentType) {
//        try {
//            LOG.info("begin to do http post");
//            LOG.debug("url=" + postUrl);
//            URL url = new URL(postUrl);
//            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//            conn.setDoOutput(true);
//            conn.setDoInput(true);
//            conn.setUseCaches(false);
//            conn.setRequestMethod("POST");
//            conn.setRequestProperty("Charset", "UTF-8");
//            // 设置文件类型:
//            conn.setRequestProperty("Content-Type", contentType);
//            conn.connect();
//            OutputStream out = conn.getOutputStream();
//            // 写入请求的字符串
//            out.write(data.getBytes());
//            out.flush();
//            out.close();
//            if (conn.getResponseCode() == 200) {
//
//                // 请求返回的数据
//                InputStream in = conn.getInputStream();
//                try {
//                    byte[] recvData = new byte[in.available()];
//                    in.read(recvData);
//                    // 转成字符串
//                    return new String(recvData);
//                } catch (Exception e) {
//                    LOG.info(e.getMessage());
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
//            LOG.error("NOT 200:" + String.valueOf(conn.getResponseCode()));
//        } catch (Exception e) {
//            LOG.info(e.getMessage());
//            e.printStackTrace();
//        }
//        return "failed";
//    }
//    public static String postMultipartform(String url, Map<String, Object> params, String contentType) throws IOException{
//        CloseableHttpClient httpclient = null;
//
//        try{
//            httpclient = HttpClients.createDefault();
//            HttpPost httpPost = new HttpPost(url);
//            httpPost.setHeader("Content-Type", contentType);
//            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//            for(Map.Entry<String, Object> entry: params.entrySet()){
//                if(entry.getValue() instanceof String)
//                    builder.addTextBody(entry.getKey(), (String)entry.getValue());
//                else if(entry.getValue() instanceof byte[])
//                    builder.addBinaryBody(entry.getKey(), (byte[])entry.getValue());
//            }
//            httpPost.setEntity(builder.build());
//            CloseableHttpResponse response = httpclient.execute(httpPost);
//            try {
//                HttpEntity entity = response.getEntity();
//
//                if (entity != null) {
//
//                    String strResponse = EntityUtils.toString(entity);
//                    EntityUtils.consume(entity);
//                    return strResponse;
//                }
//                throw new IOException("http Post failed");
//            } finally {
//                response.close();
//            }
//        }
//        catch (IOException e){
//            throw e;
//        }
//        finally {
//            if(httpclient != null){
//                try {
//                    httpclient.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

}
